package entity.lemming;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import entity.Entity;
import entity.terrain.Terrain;
import level.*;

/**
 * The Basher Lemming, a Lemming that digs horizontally through the terrain.
 * 
 * @author Neil Cowzer
 */
public class Basher extends Lemming {
	
	

	/**
	 * Create an instance of the Basher Lemming.
	 * 
	 * @param x the starting x-coordinate.
	 * @param y the starting y-coordinate.
	 * @param level the current level.
	 */
	/*@ requires 0 <= x && 0 <= y;
		assignable speed_X && speed_Y && energy;
		ensures speed_X != 0 && speed_Y == 0 && energy == 100;
	 */
	public Basher(int x, int y){
			super("resource/graphics/lemming/basher/basher_1.gif",19, x, y);
			speed_X = 10;
			speed_Y = 0;
			energy = 100;
	}
	
	/**
	 * Create a new Blocker with the given center point
	 * 
	 * @param center point of the Lemming
	 */
	public Basher(Point center){
		super("resource/graphics/lemming/basher/basher_1.gif",19, center);
		speed_X = 1;
		speed_Y = 0;
		energy = 30;
	}


	/**
	 * Checks for a collision with another Entity
	 * 
	 * @param other another Entity eg Terrain, Lemming.
	 * @return true if a collision has occured.
	 */
	public /*@ pure @*/ boolean collision(Lemming other) {
		Rectangle me = getBounds();
		if (other instanceof Blocker){
			if (me.intersects(other.getBounds())){
				//return true;
			}
		}
		
		return false;
	}

	/** Perform the logic of moving a Lemming. */
	//@ ensures x = \old(x) + speed_X && energy == \old(energy) -1;
	public void move(Terrain terrain) {
		boolean bash = false;
		Rectangle lemming = getBounds();
		
		if (lemming.intersects(terrain.getBounds())){
			BufferedImage image = ((Terrain)terrain).getImage().getBufferedImage();
			int black = -16777216; // black that represents the background
			int black2 = -16776960;
			try{
				if (speed_X > 0){
					// if the is at least one non- black pixel - dig
					for (int i = getY(); i < getY() + lemming.height; i++){
						int color = image.getRGB(x+lemming.width, i);

						if (color != black){
							if (color != black){
								if(color != black2){
									bash = true;
								}else {
									bash = true;
								}
							}
							break;
						}

						/* Allow for a little bit of freedom,
						 * if the lemming is "close" to the wall.
						 */ 
						color = image.getRGB(x+lemming.width+1, i);
						if (color != black){
							if (color != black){
								if(color != black2){
									bash = true;
								}else {
									bash = true;
								}
							}
							break;
						}

						color = image.getRGB(x+lemming.width+2, i);
						if (color != black){
							if (color != black){
								if(color != black2){
									bash = true;
								}else {
									bash = true;
								}
							}
							break;
						}
					}


					if (bash){
						if (energy == 30){
							for (int i = x; i < x + lemming.width; i++){
								terrain.destroyTerrain(i, y, "basher");
							}
						}
						terrain.destroyTerrain(x+lemming.width, y, "basher");
					} else {
						energy = 1;
					}
				}else{
					// if the is at least one non- black pixel - dig
					for (int i = getY(); i < getY() + lemming.height; i++){
						int color = image.getRGB(x, i);

						if (color != black){
							if (color != black){
								if(color != black2){
									bash = true;
								}else {
									bash = true;
								}
							}
							break;
						}
						/* Allow for a little bit of freedom,
						 * if the lemming is "close" to the wall.
						 */ 
						color = image.getRGB(x-1, i);
						if (color != black){
							if (color != black){
								if(color != black2){
									bash = true;
								}else {
									bash = true;
								}
							}
							break;
						}

						color = image.getRGB(x-2, i);
						if (color != black){
							if (color != black){
								if(color != black2){
									bash = true;
								}else {
									bash = true;
								}
							}
							break;
						}
					}


					if (bash){
						if (energy == 30){
							for (int i = x; i < x + lemming.width; i++){
								terrain.destroyTerrain(i, y, "basher");
							}
						}
						terrain.destroyTerrain(x-1, y, "basher");
					} else {
						energy = 1;
					}
				}
			}catch(ArrayIndexOutOfBoundsException e){}
		}
		
				//update positions
		x= x+speed_X;
		update();
		energy--;
	}
	
	/** Reverse a Lemmings direction */
	/*@ ensures speed_X == 0 - \old(speed_X) &&
	 			speed_Y == 0 - \old(speed_Y);
	@*/
	public void turnAround(){
		speed_X = -(speed_X);
		
		generateFrames("resource/graphics/lemming/basher/basher_1.gif",19);
		if (speed_X < 0){
			generateFrames("resource/graphics/lemming/basher/Leftbasher_1.gif",19);
		}
	}

}
